import django_filters
from .models import DataSource, OPCUASubscription, MQTTSubscription

class DataSourceFilter(django_filters.FilterSet):
    """
    数据源过滤器
    提供按名称和类型过滤数据源的功能
    """
    name = django_filters.CharFilter(
        lookup_expr='icontains', 
        label='数据源名称',
        help_text='支持模糊搜索'
    )
    type = django_filters.ChoiceFilter(
        choices=DataSource.TYPE_CHOICES, 
        label='数据源类型'
    )

    class Meta:
        model = DataSource
        fields = ['name', 'type']

class OPCUASubscriptionFilter(django_filters.FilterSet):
    """
    订阅项过滤器
    提供按数据源、节点ID和状态过滤订阅项的功能
    """
    data_source = django_filters.ModelChoiceFilter(
        queryset=DataSource.objects.all(),
        label='数据源'
    )
    node_id = django_filters.CharFilter(
        lookup_expr='icontains', 
        label='节点ID/主题',
        help_text='支持模糊搜索'
    )
    is_active = django_filters.BooleanFilter(
        label='是否启用'
    )

    class Meta:
        model = OPCUASubscription
        fields = ['data_source', 'node_id', 'is_active']

class MQTTSubscriptionFilter(django_filters.FilterSet):
    """
    订阅项过滤器
    提供按数据源、节点ID和状态过滤订阅项的功能
    """
    data_source = django_filters.ModelChoiceFilter(
        queryset=DataSource.objects.all(),
        label='数据源'
    )
    topic = django_filters.CharFilter(
        lookup_expr='icontains', 
        label='主题',
        help_text='支持模糊搜索'
    )
    is_active = django_filters.BooleanFilter(
        label='是否启用'
    )

    class Meta:
        model = MQTTSubscription
        fields = ['data_source', 'topic', 'is_active'] 